﻿Public Class frmSourceViewer

    Public FileToShow As String
    Public TextSearch As String
    Public LineFound As Long
    Public SensitiveSearch As Boolean


    Private Sub frmSourceViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.ToolStrip1.Items.Add(Me.ToolStripButton1)
        Me.ToolStrip1.Items.Add(Me.ToolStripButton2)


        If FileToShow <> "" Then

            Try
                Me.Text = FileToShow

                Dim itemStrRead As New System.IO.StreamReader(FileToShow, System.Text.Encoding.Default)
                Dim lLinea As Long = 0

                While Not itemStrRead.EndOfStream
                    lLinea += 1
                    ListBox1.Items.Add(CStr(lLinea) & ": " & itemStrRead.ReadLine)
                End While
                itemStrRead.Close()

                If LineFound Then
                    ListBox1.SelectedIndex = LineFound - 1

                End If


            Catch ex As Exception
                MsgBox("Can't open file: " & vbCrLf & FileToShow & vbCrLf & ex.Message.ToString)
            End Try

        End If

    End Sub

    Private Sub frmSourceViewer_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        RedimForm()

    End Sub

    Private Sub RedimForm()

        Try

            Me.ListBox1.Top = Me.ToolStrip1.Height + 1
            Me.ListBox1.Left = 0
            Me.ListBox1.Width = Me.ClientSize.Width
            Me.ListBox1.Height = Me.ClientSize.Height - Me.ToolStrip1.Height

        Catch ex As Exception

        End Try

    End Sub


    Private Sub Beginning(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

        If ListBox1.Items.Count > 0 Then

            Dim j As Integer

            If SensitiveSearch Then
                j = ListBox1.FindStringExact(TextSearch, 0)
            Else
                j = ListBox1.FindString(TextSearch, 0)
            End If

            If j > 0 Then ListBox1.SelectedIndex = j
        End If

    End Sub

    Private Sub Nexted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

        If ListBox1.Items.Count > 0 Then
            Dim i As Integer = ListBox1.SelectedIndex
            Dim j As Integer

            If SensitiveSearch Then
                j = ListBox1.FindStringExact(TextSearch, i + 1)
            Else
                j = ListBox1.FindString(TextSearch, i + 1)
            End If


            If j > 0 Then
                ListBox1.SelectedIndex = j
            End If
        End If

    End Sub

    Private Function Find(ByVal Text As String, ByVal Pos As Long) As Long

        For i As Long = Pos To ListBox1.Items.Count - 1

            If InStr(ListBox1.Items(i), Text) Then
                Return i
            End If

        Next

        Return -1

    End Function
End Class